package org.example.Polls;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MockPollService implements PollService {
	
	private static long pollCount = 1L;
	
	private List<Poll> polls;
	
	private static long nextPollCount() {
		return pollCount++;
	}
	
	public MockPollService() {
		polls = new ArrayList<Poll>();
		
		Poll poll1 = new Poll();
		poll1.setId(nextPollCount());
		
		User admin = new User();
		admin.setCreateDate(new Date());
		admin.setAlias("Mario");
		admin.setEmail("mario@uvote.com");
		admin.setFirstName("Mario");
		admin.setLastName("Smith");
		admin.setId(1L);
		admin.setPassword("password");
		
		User jason = new User();
		jason.setCreateDate(new Date());
		jason.setAlias("Jason");
		jason.setEmail("jason@uvote.com");
		jason.setFirstName("Jason");
		jason.setLastName("Alexander");
		jason.setId(2L);
		jason.setPassword("password");
		
		Set<PollItem> items = new HashSet<PollItem>();
		Set<PollParticipant> participants = new HashSet<PollParticipant>();
		
		PollItem item1 = new PollItem();
		item1.setCreateDate(new Date());
		item1.setCreator(admin);
		item1.setId(1L);
		item1.setNumVotes(3);
		item1.setPollId(poll1.getId());
		item1.setText("Tebow won't let us down.");
		items.add(item1);
		
		PollItem item2 = new PollItem();
		item2.setCreateDate(new Date());
		item2.setCreator(admin);
		item2.setId(2L);
		item2.setNumVotes(7);
		item2.setPollId(poll1.getId());
		item2.setText("Tebow couldn't fight his way out of a wet paperbag!");
		items.add(item2);
		
		PollParticipant participant1 = new PollParticipant();
		participant1.setEmail(jason.getEmail());
		participant1.setId(1L);
		participant1.setPollId(poll1.getId());
		participant1.setUser(jason);
		participant1.setVote(null);
		participant1.setVoteDate(null);
		participants.add(participant1);
		
		poll1.setAdministrator(admin);
		poll1.setAnonymous(false);
		poll1.setCreateDate(new Date());
		poll1.setDynamic(false);
		poll1.setEndDate(new Date(System.currentTimeMillis()+86400000));
		poll1.setItems(items);
		poll1.setParticipants(participants);
		poll1.setPublc(false);
		poll1.setStartDate(new Date());
		poll1.setTitle("Will Tebow do it again?");
		polls.add(poll1);
	}

	public Poll createPoll(RequestContext ctxt, Poll poll) throws Exception {
		User admin = new User();
		admin.setEmail(ctxt.getUsername());
		admin.setPassword(ctxt.getPassword());
		poll.setAdministrator(admin);
		poll.setCreateDate(new Date());
		poll.setId(nextPollCount());
		for (PollItem item: poll.getItems()) {
			item.setNumVotes(3);
		}
		this.polls.add(poll);
		return poll;
	}

	public Poll getPoll(RequestContext ctxt, Long pollId,
			boolean participantDetails, boolean itemDetails) throws Exception {
		for (Poll poll: this.polls) {
			if (pollId.equals(poll.getId())) {
				return poll;
			}
		}
		return null;
	}

	public PollParticipant vote(RequestContext ctxt, Long pollId,
			Long pollItemId) throws Exception {
		return null;
	}

	public PollParticipant addParticipant(RequestContext ctxt, String email,
			Long pollId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public PollItem addItem(RequestContext ctxt, String itemValue, Long pollId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Poll> findMyPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		List<Poll> adminPolls = new ArrayList<Poll>();
		for (Poll poll : this.polls) {
			if (ctxt.getUsername().equals(poll.getAdministrator().getEmail())) {
    			adminPolls.add(poll);
			}
		}
		return adminPolls;
	}

	public List<Poll> findParticipantPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		List<Poll> participantPolls = new ArrayList<Poll>();
		for (Poll poll : this.polls) {
			for (PollParticipant participant: poll.getParticipants()) {
    			if (ctxt.getUsername().equals(participant.getEmail())) {
        			participantPolls.add(poll);
    			}
			}
		}
		return participantPolls;
	}

	public List<Poll> findPublicPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		List<Poll> publicPolls = new ArrayList<Poll>();
		for (Poll poll : this.polls) {
			if (poll.getPublc()) {
				publicPolls.add(poll);
			}
		}
		return publicPolls;
	}

}
